<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="员工姓名" prop="userName">
        <el-input v-model="formData.userName" placeholder="请输入员工姓名" />
      </el-form-item>
      <el-form-item label="考核月份" prop="yearMonthValue">
        <el-input v-model="formData.yearMonthValue" placeholder="请输入考核月份(格式:YYYY-MM)" />
      </el-form-item>
      <el-form-item label="准确性分值" prop="accuracyScore">
        <el-input v-model="formData.accuracyScore" placeholder="请输入准确性分值" />
      </el-form-item>
      <el-form-item label="效率分值" prop="efficiencyScore">
        <el-input v-model="formData.efficiencyScore" placeholder="请输入效率分值" />
      </el-form-item>
      <el-form-item label="合规性分值" prop="complianceScore">
        <el-input v-model="formData.complianceScore" placeholder="请输入合规性分值" />
      </el-form-item>
      <el-form-item label="创新性分值" prop="innovationScore">
        <el-input v-model="formData.innovationScore" placeholder="请输入创新性分值" />
      </el-form-item>
      <el-form-item label="团队协作分值" prop="teamworkScore">
        <el-input v-model="formData.teamworkScore" placeholder="请输入团队协作分值" />
      </el-form-item>
      <el-form-item label="客户反馈分值" prop="clientFeedbackScore">
        <el-input v-model="formData.clientFeedbackScore" placeholder="请输入客户反馈分值" />
      </el-form-item>
      <el-form-item label="工作量分值" prop="workloadScore">
        <el-input v-model="formData.workloadScore" placeholder="请输入工作量分值" />
      </el-form-item>
      <el-form-item label="特殊加分" prop="specialPlusScore">
        <el-input v-model="formData.specialPlusScore" placeholder="请输入特殊加分" />
      </el-form-item>
      <el-form-item label="特殊减分" prop="specialMinusScore">
        <el-input v-model="formData.specialMinusScore" placeholder="请输入特殊减分" />
      </el-form-item>
      <el-form-item label="总分" prop="totalScore">
        <el-input v-model="formData.totalScore" placeholder="请输入总分" />
      </el-form-item>
      <el-form-item label="评分说明" prop="scoreComment">
        <el-input v-model="formData.scoreComment" placeholder="请输入评分说明" />
      </el-form-item>
      <el-form-item label="评分人ID" prop="assessorId">
        <el-input v-model="formData.assessorId" placeholder="请输入评分人ID" />
      </el-form-item>
      <el-form-item label="评分人姓名" prop="assessorName">
        <el-input v-model="formData.assessorName" placeholder="请输入评分人姓名" />
      </el-form-item>
      <el-form-item label="评分时间" prop="assessTime">
        <el-date-picker
          v-model="formData.assessTime"
          type="date"
          value-format="x"
          placeholder="选择评分时间"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { HrFinanceAssessmentDetailApi, HrFinanceAssessmentDetailVO } from '@/api/bpm/hrfinanceassessmentdetail'

/** 财税部月度考核分值明细 表单 */
defineOptions({ name: 'HrFinanceAssessmentDetailForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  userName: undefined,
  yearMonthValue: undefined,
  accuracyScore: undefined,
  efficiencyScore: undefined,
  complianceScore: undefined,
  innovationScore: undefined,
  teamworkScore: undefined,
  clientFeedbackScore: undefined,
  workloadScore: undefined,
  specialPlusScore: undefined,
  specialMinusScore: undefined,
  totalScore: undefined,
  scoreComment: undefined,
  assessorId: undefined,
  assessorName: undefined,
  assessTime: undefined,
})
const formRules = reactive({
  userName: [{ required: true, message: '员工姓名不能为空', trigger: 'blur' }],
  yearMonthValue: [{ required: true, message: '考核月份', trigger: 'blur' }],
  accuracyScore: [{ required: true, message: '准确性分值不能为空', trigger: 'blur' }],
  efficiencyScore: [{ required: true, message: '效率分值不能为空', trigger: 'blur' }],
  complianceScore: [{ required: true, message: '合规性分值不能为空', trigger: 'blur' }],
  innovationScore: [{ required: true, message: '创新性分值不能为空', trigger: 'blur' }],
  teamworkScore: [{ required: true, message: '团队协作分值不能为空', trigger: 'blur' }],
  clientFeedbackScore: [{ required: true, message: '客户反馈分值不能为空', trigger: 'blur' }],
  workloadScore: [{ required: true, message: '工作量分值不能为空', trigger: 'blur' }],
  specialPlusScore: [{ required: true, message: '特殊加分不能为空', trigger: 'blur' }],
  specialMinusScore: [{ required: true, message: '特殊减分不能为空', trigger: 'blur' }],
  totalScore: [{ required: true, message: '总分不能为空', trigger: 'blur' }],
  assessorId: [{ required: true, message: '评分人ID不能为空', trigger: 'blur' }],
  assessorName: [{ required: true, message: '评分人姓名不能为空', trigger: 'blur' }],
  assessTime: [{ required: true, message: '评分时间不能为空', trigger: 'blur' }],
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await HrFinanceAssessmentDetailApi.getHrFinanceAssessmentDetail(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as HrFinanceAssessmentDetailVO
    if (formType.value === 'create') {
      await HrFinanceAssessmentDetailApi.createHrFinanceAssessmentDetail(data)
      message.success(t('common.createSuccess'))
    } else {
      await HrFinanceAssessmentDetailApi.updateHrFinanceAssessmentDetail(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    userName: undefined,
    yearMonthValue: undefined,
    accuracyScore: undefined,
    efficiencyScore: undefined,
    complianceScore: undefined,
    innovationScore: undefined,
    teamworkScore: undefined,
    clientFeedbackScore: undefined,
    workloadScore: undefined,
    specialPlusScore: undefined,
    specialMinusScore: undefined,
    totalScore: undefined,
    scoreComment: undefined,
    assessorId: undefined,
    assessorName: undefined,
    assessTime: undefined,
  }
  formRef.value?.resetFields()
}
</script>
